# Maintainer: Michał Łukowski <michal.lukowski@gmail.com>

_realname=abseil-cpp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=20250512.0
pkgrel=1
pkgdesc="Abseil Common Libraries (C++) from Google (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://abseil.io"
msys2_repository_url="https://github.com/abseil/abseil-cpp"
msys2_references=(
  'archlinux: abseil-cpp'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/abseil/abseil-cpp/archive/$pkgver/$_realname-$pkgver.tar.gz"
        "0001-abseil-Fix-compiler-warnings.patch"
        "0002-abseil-Remove-librt-library.patch"
        "0003-fix-linking-abseil_dll.patch")
sha256sums=('7262daa7c1711406248c10f41026d685e88223bc92817d16fb93c19adb57f669'
            '30fb7526d1c89f722dd0ce0c467e034370140fbf07a1ad4f91df301c7f442551'
            '8946282db6a225b385729deb345b57e3ee75f4e859dbbc49ab7bc5604d71192f'
            '67ee0328f1aa8e3ad24231b2e6ef6a78963218e087cc6a7230e2d4e61b27a741')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"

  apply_patch_with_msg \
    0001-abseil-Fix-compiler-warnings.patch \
    0002-abseil-Remove-librt-library.patch \
    0003-fix-linking-abseil_dll.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DABSL_PROPAGATE_CXX_STD=ON \
      -DCMAKE_CXX_STANDARD=17 \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
